package solicitudesApp.GUI;

import sMySQLappTemplate.GUI.MyBorder;
import sMySQLappTemplate.GUI.MyButtonPanel;
import sMySQLappTemplate.GUI.MyMenuBar;
import sMySQLappTemplate.GUI.MyTable;
import solicitudesApp.Core.ApplicationsAppCore;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public class MainWindowSolicitudes extends sMySQLappTemplate.GUI.MainWindowTemplate
{	
	private JTable tablaSolicitudes;
	private JTable tablaPedidos;
	private JTable tablaRequisitos;
	
	public MyButtonPanel applicationsTools;
	public MyButtonPanel requestsTools; 
	public MyButtonPanel requirementsTools;
	
	public JMenuBar mainMenuBar;
	
	public ApplicationsAppCore appCore;
	
	public MainWindowSolicitudes(ApplicationsAppCore AppCore)
	{
		super();
		
		this.appCore = AppCore;
		
		this.setTitle("Gestor de Solicitudes");
		
		// INIT COMPONENTS :P
		
		JSplitPane basePanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		basePanel.setDividerSize(3);
		this.setContentPane(basePanel);
		
		// Barra de Menues
		mainMenuBar = new MyMenuBar();		
		this.setJMenuBar(mainMenuBar);
		
		// Panel de Seleccion de Solicitudes	
		JPanel applicationsPane = new JPanel(new BorderLayout());
		applicationsPane.setPreferredSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
		applicationsPane.setBorder(new MyBorder(4,"Solicitudes"));
		
		this.getContentPane().add(applicationsPane);
		
		// Display para la tabla de Solicitudes		
		JScrollPane applicationsDisplay = new JScrollPane();
		applicationsDisplay.setBorder(BorderFactory.createEmptyBorder());
		applicationsDisplay.setPreferredSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
		applicationsDisplay.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		applicationsDisplay.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		tablaSolicitudes = new MyTable(appCore.getTablaSolicitudes());
		tablaSolicitudes.getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent arg0) 
			{
				int row = tablaSolicitudes.getSelectedRow();
				if (row == -1) return;
				appCore.tablaSolicitudesSelectionChanged(row);				
			}			
		});
		applicationsDisplay.setViewportView(tablaSolicitudes);

		applicationsPane.add(applicationsDisplay, BorderLayout.CENTER);
		
		// Panel de Herramientos de Solicitud y Filtros
		applicationsTools = new MyButtonPanel();
		
		applicationsPane.add(applicationsTools, BorderLayout.SOUTH);
		
		// Paneles de Seleccion de Contenido de Solicitud		
		JSplitPane applicationContentPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		applicationContentPane.setPreferredSize(new Dimension(240, Integer.MAX_VALUE));
		applicationContentPane.setMinimumSize(new Dimension(200, Integer.MAX_VALUE));
		applicationContentPane.setBorder(null);
		applicationContentPane.setDividerSize(3);
		
		this.getContentPane().add(applicationContentPane);
		
		// Panel de Requisitos de Solicitud
		JPanel applicationRequests = new JPanel(new BorderLayout());
		applicationRequests.setBorder(new MyBorder(4,"Pedidos"));
		applicationRequests.setPreferredSize(new Dimension(240, 260));
		
		applicationContentPane.add(applicationRequests);
		
		// Display de Requisitos
		JScrollPane requestsDisplay = new JScrollPane();
		requestsDisplay.setBorder(BorderFactory.createEmptyBorder());
		requestsDisplay.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		tablaPedidos = new MyTable(appCore.getTablaPedidos());
		requestsDisplay.setViewportView(tablaPedidos);
		
		applicationRequests.add(requestsDisplay, BorderLayout.CENTER);
		
		// Herramientas para Requisitos
		requestsTools = new MyButtonPanel();
	
		applicationRequests.add(requestsTools, BorderLayout.SOUTH);

		// Panel de Pedidos en Solicitud		
		JPanel applicationRequirements = new JPanel(new BorderLayout());
		applicationRequirements.setBorder(new MyBorder(4,"Requisitos"));
		applicationRequirements.setPreferredSize(new Dimension(240, 260));		
		
		applicationContentPane.add(applicationRequirements);	
		
		// Display de Pedidos
		JScrollPane requirementsDisplay = new JScrollPane();
		requirementsDisplay.setBorder(BorderFactory.createEmptyBorder());
		requirementsDisplay.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		tablaRequisitos = new MyTable(appCore.getTablaRequisitos());
		requirementsDisplay.setViewportView(tablaRequisitos);
		
		applicationRequirements.add(requirementsDisplay, BorderLayout.CENTER);
		
		// Herramientas para Pedidos
		requirementsTools = new MyButtonPanel();
	
		applicationRequirements.add(requirementsTools, BorderLayout.SOUTH);	
	}

	public int getTablaSolicitudSelectedRow(){
		return this.tablaSolicitudes.getSelectedRow();
	}
}
